<?php
class InicijalizatorSustava {
    // property declaration
    //public $var = 'a default value';

    // method declaration
	public function inicijaliziraj() {
		
		$servername = "localhost";
		$username = "root";
		$password = "";

		// Create connection
		$conn = new mysqli($servername, $username, $password);
		// Check connection
		if ($conn->connect_error) {
			die("Connection failed: " . $conn->connect_error);
		}

		$sql = "drop database if exists bazaMinistarstava";
		if ($conn->query($sql) === TRUE) {
		    echo "Database dropped successfully</br>";
		} else {
		    echo "Error creating database: " . $conn->error;
		}

		// Create database
		$dbName = "bazaMinistarstava";
		$sql = "CREATE DATABASE " . $dbName;
		if ($conn->query($sql) === TRUE) {
		    echo "Database created successfully</br>";
		} else {
		    echo "Error creating database: " . $conn->error;
		}

		$conn->close();

		$conn = new mysqli($servername, $username, $password, $dbName);
		if ($conn->connect_error) {
			die("Connection failed: " . $conn->connect_error);
		}

		if (!$conn->set_charset("utf8")) {
		    printf("Error loading character set utf8: %s</br>", $conn->error);
		} else {
		    printf("Current character set: %s</br>", $conn->character_set_name());
		}

		$sql = "create table ministarstvo (
			nazivMinistarstva char(100) primary key,
			adrMinistarstva char(100),
			telMinistarstva char(20),
			emailMinistarstva char(100),
			povMinistarstva text
			)";

		if ($conn->query($sql) === TRUE) {
		    echo "Table ministarstvo created successfully</br>";
		} else {
		    echo "Error creating table: " . $conn->error;
		}

		$sql = "create table djelatnik (
			sifDjel smallint primary key,
			ime char(20),
			prezime char(20),
			titula char(20),
			email char(100),
			funkcija char(100),
			stupanjID smallint
			)";

		if ($conn->query($sql) === TRUE) {
		    echo "Table djelatnik created successfully</br>";
		} else {
		    echo "Error creating table: " . $conn->error;
		}

		$sql = "create table upit (
			upidID integer primary key,
			sifDjel smallint,
			tekst text,
			kategorijaID smallint,
			oib char(11)
			)";

		if ($conn->query($sql) === TRUE) {
		    echo "Table upit created successfully</br>";
		} else {
		    echo "Error creating table: " . $conn->error;
		}

		$sql = "create table akcija (
			akcijaID integer primary key,
			naslov char(20),
			opis text,
			brPrijavljenih integer,
			brDopuštenih integer
			)";

		if ($conn->query($sql) === TRUE) {
		    echo "Table akcija created successfully</br>";
		} else {
		    echo "Error creating table: " . $conn->error;
		}

		//čemu služi vrijeme posljednje prijave?
		$sql = "create table gradanin (
			oib char(11) primary key,
			ime char(20),
			prezime char(20),
			email char(100),
			vrPrijave datetime
			)";

		if ($conn->query($sql) === TRUE) {
		    echo "Table gradanin created successfully</br>";
		} else {
		    echo "Error creating table: " . $conn->error;
		}

		$sql = "create table poruke (
			porukaID integer primary key,
			sifPrimatelj integer,
			sifPosiljatelj integer,
			poruka text,
			privitak blob
			)";

		if ($conn->query($sql) === TRUE) {
		    echo "Table poruke created successfully</br>";
		} else {
		    echo "Error creating table: " . $conn->error;
		}

		$sql = "create table arhiva (
			ime char(100) primary key,
			sadrzaj blob,
			stupanjID smallint
			)";

		if ($conn->query($sql) === TRUE) {
		    echo "Table arhiva created successfully</br>";
		} else {
		    echo "Error creating table: " . $conn->error;
		}

		$sql = "create table stupanj (
			stupanjID smallint primary key
			)";

		if ($conn->query($sql) === TRUE) {
		    echo "Table stupanj created successfully</br>";
		} else {
		    echo "Error creating table: " . $conn->error;
		}

		$sql = "create table kategorija (
			kategorijaID smallint primary key,
			kategorijaOpis char(100)
			)";

		if ($conn->query($sql) === TRUE) {
		    echo "Table kategorija created successfully</br>";
		} else {
		    echo "Error creating table: " . $conn->error;
		}

		$sql = "create table akcijaGradanin (
			akcijaID integer,
			oib char(11),
			vrPrijave datetime,
			primary key (akcijaID, oib)
			)";

		if ($conn->query($sql) === TRUE) {
		    echo "Table akcijaGradanin created successfully</br>";
		} else {
		    echo "Error creating table: " . $conn->error;
		}

		$sql = "create table djelatnikPoruke (
			porukaID integer primary key,
			sifDjel integer
			)";

		if ($conn->query($sql) === TRUE) {
		    echo "Table djelatnikPoruke created successfully</br>";
		} else {
		    echo "Error creating table: " . $conn->error;
		}

        /////////////////////////////////
		//ovdje počinje unošenje podataka
		/////////////////////////////////

		$sql = "insert into ministarstvo values (
			'Ministarstvo Financija', 'Pojma nemam 72', '1234', 'asdf@xyz.hr', 'ovo ž ć č đ š je povijest ministarstva.')";

		if ($conn->query($sql) === TRUE) {
		    echo "New record created successfully</br>";
		} else {
		    echo "Error: " . $sql . "<br>" . $conn->error;
		}

		$sql = "insert into djelatnik values (
			1, 'Neki', 'Lik', 'Titula', 'asdf@qwer.hr', 'Član uprave')";

		if ($conn->query($sql) === TRUE) {
		    echo "New record created successfully</br>";
		} else {
		    echo "Error: " . $sql . "<br>" . $conn->error;
		}

		$conn->close();

	}
}
?>